package com.atu.rtc.factory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.util.DisplayMetrics;
import android.view.View;

import org.java_websocket.client.WebSocketClient;
import org.webrtc.IceCandidate;
import org.webrtc.ScreenCapturerAndroid;
import org.webrtc.SessionDescription;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;

public class ScreenCapturer extends AbstractCapturer implements ICapturer {

    private Activity activity;

    private Intent intent;
    private DisplayMetrics metrics = new DisplayMetrics();
    private int requestCode;
    private WebSocketClient client;

    private View cover;

    public ScreenCapturer(Activity activity, View cover, int requestCode) {
        this.activity = activity;
        this.cover = cover;
        this.requestCode = requestCode;
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    }

    @Override
    public void init(WebSocketClient client,SurfaceViewRenderer surfaceViewRenderer) {
        this.client = client;
        this.localSurfaceView = surfaceViewRenderer;
        MediaProjectionManager projectionManager = (MediaProjectionManager) getActivity().getSystemService(Context.MEDIA_PROJECTION_SERVICE);

        Intent captureIntent = projectionManager.createScreenCaptureIntent();
        getActivity().startActivityForResult(captureIntent, requestCode);

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ScreenCapturer.this.cover.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void setIntent(Intent intent) {
        this.intent = intent;
    }

    @Override
    public void connect() {
        createPeerConnection(localSurfaceView);
    }

    @Override
    public void createAnswer() {
        super.createAnswer(peerConnection);
    }

    @Override
    public void createOffer() {
        super.createOffer(peerConnection);
    }

    @Override
    public void setRemoteDescription(SessionDescription sessionDescription) {
        peerConnection.setRemoteDescription(observer, sessionDescription);
    }

    @Override
    public void addIceCandidate(IceCandidate iceCandidate) {
        peerConnection.addIceCandidate(iceCandidate);
    }

    @Override
    public void close() {
        super.close();
    }

    @Override
    protected int getWidth() {
        return metrics.widthPixels;
    }

    @Override
    protected int getHeight() {
        return metrics.heightPixels;
    }

    @Override
    protected Activity getActivity() {
        return activity;
    }

    @Override
    protected VideoCapturer createVideoCapturer() {
        return new ScreenCapturerAndroid(intent, new MediaProjection.Callback() {
            @Override
            public void onStop() {
            }
        });
    }

    @Override
    protected WebSocketClient getClient() {
        return client;
    }
}
